package DAO;

import java.awt.Font;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

import Main.DBMain;

public class DelStudent extends JFrame {
	JPanel contentPane = new JPanel();
	JLabel lab_Tip1, lab_Tip2;
	JTextField stuNoField;
	JTextArea infoShow;
	JButton btn_Del, btn_Exit;

	public DelStudent() {
		setTitle("删除学生");
		setSize(600, 300);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Tip1 = new JLabel("请输入要删除学生的学号：");
		lab_Tip1.setBounds(200, 10, 200, 20);
		lab_Tip1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		contentPane.add(lab_Tip1);

		stuNoField = new JTextField();
		stuNoField.setBounds(200, 40, 190, 30);
		/* 添加回车监听，点击删除学生 */
		stuNoField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					delStu();
			}
		});
		contentPane.add(stuNoField);

		btn_Del = new JButton("删除");
		btn_Del.setBounds(200, 80, 70, 30);
		btn_Del.setFont(new Font("微软雅黑", Font.BOLD, 16));
		/* 添加按钮监听，点击删除学生 */
		btn_Del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				delStu();
			}
		});
		contentPane.add(btn_Del);

		btn_Exit = new JButton("取消");
		btn_Exit.setBounds(320, 80, 70, 30);
		btn_Exit.setFont(new Font("微软雅黑", Font.BOLD, 16));
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		contentPane.add(btn_Exit);

		JLabel lab_OK = new JLabel("删除学生成功，删除的学生信息为：");
		lab_OK.setBounds(170, 120, 260, 20);
		lab_OK.setFont(new Font("楷体", Font.PLAIN, 16));
		contentPane.add(lab_OK);

		infoShow = new JTextArea();
		infoShow.setBounds(5, 140, 575, 120);
		contentPane.add(infoShow);

		setVisible(true);
	}

	public void delStu() {
		Connection conn = null;
		java.sql.PreparedStatement pstmt = null;
		ResultSet rs = null;
		String delStuNo = stuNoField.getText().trim();
		try {
			conn = DBMain.getConnection();
			String sql = "select * from stu_info where stuNo='" + delStuNo + "'";
			pstmt = conn.prepareStatement(sql);
			// 获取该学生所有信息，展示
			rs = pstmt.executeQuery(sql);
			ResultSetMetaData colName = rs.getMetaData();
			if (rs.next()) {
				for (int i = 1; i < colName.getColumnCount(); i++) {
					infoShow.append(colName.getColumnName(i) + "\t");
				}
				infoShow.append("\n");
				for (int i = 1; i < colName.getColumnCount(); i++) {
					infoShow.append(rs.getString(i) + "\t");
				}
				infoShow.append("\n");
			} else {
				JOptionPane.showMessageDialog(null, "该学生不存在！请重新输入");
				stuNoField.setText("");
			}
			// 删除该学生
			String sql1 = "delete from stu_info where stuNo=?";
			pstmt = conn.prepareStatement(sql1);
			pstmt.setString(1, delStuNo);
			int flag = pstmt.executeUpdate();
			// 记录删除结果
			if (flag != 0) {
				JOptionPane.showMessageDialog(null, "删除成功！");
			} else {
				JOptionPane.showMessageDialog(null, "学生不存在，删除失败！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
